package com.gnerv.sylvanas.module.datasource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
public class FrameworkPluginDatasourceEnvironmentPostProcessor implements EnvironmentPostProcessor {

    private static final String NAME = "PluginDatasourceProperties";
    private static final Map<Object, Object> ENVIRONMENT = new ConcurrentHashMap<>();

    static {
        ENVIRONMENT.put("spring.datasource.dynamic.primary.strict", false);
    }

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Properties properties = new Properties();
        properties.putAll(ENVIRONMENT);
        PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(NAME, properties);
        environment.getPropertySources().addLast(propertiesPropertySource);
    }
}
